/**
 * Created by Administrator on 2017/4/1 0001.
 */
import React from "react";
import { Modal ,Checkbox} from 'antd';
import GetGoodsListMutation from "../mutation/GetGoodsListMutation";
import Relay from "react-relay";
import AaaESMItem from "../AaaESMItem"
import ReactKey from "../../../util/ReactKey"
import Confirm from "../../../common/Confirm"
import Util from "../../../util/Util"
export default class AddExistStyleModal extends React.Component{
    constructor(props){
        super(props)
        this.state={
            visible:false,
            goodsInfoDescModelList: [],
        }

        this.orders=[];
        this.enterpriseId = sessionStorage.getItem('enterpriseId')
    }



    componentWillMount(){
        let categoryId = '';
        let num = this.props.ordertype_num
        if(num==1){
            categoryId = 1
        }else if(num==2){
            categoryId = 4
        }else if(num==3){
            categoryId = 2
        }else if(num==4){
            categoryId = ''
        }else if(num==5){
            categoryId = 3
        }



        Relay.Store.commitUpdate(
            new GetGoodsListMutation({
                companyId : this.enterpriseId,
                categoryId:categoryId
            }),
            {
                onFailure: () => {

                },
                onSuccess: (response) => {
                    // console.warn(JSON.stringify(response))
                    let errorcode = response.getGoodsList.responseData.errorCode;
                    if(errorcode==0){
                        let goodsInfoDescModelList = response.getGoodsList.responseData.goodsInfoDescModelList
                        if(!goodsInfoDescModelList){
                            goodsInfoDescModelList = []
                        }
                        this.setState({
                            goodsInfoDescModelList : goodsInfoDescModelList
                        });
                    }

                }
            }
        )
    }

    viewHead() {
        switch (this.props.ordertype_num){
            case '1':
                return(
                  <tr className="order_from_tr">
                      <td className="order_frorm_td order_from_td_num "><TotalCheckBox ref={'checkall'} checkall={this.checkall.bind(this)}/></td>
                      {/*<td className="order_frorm_td order_from_td_snum order_detail_thead_style">款号</td>*/}
                      <td className="order_frorm_td order_from_td_cnum order_detail_thead_style">品名</td>
                      <td className="order_frorm_td order_from_td_describe order_detail_thead_style">描述</td>
                      <td className="order_frorm_td order_from_td_monad order_detail_thead_style">颜色</td>
                      {/*<td className="order_frorm_td order_from_td_img order_detail_thead_style">花型图片</td>*/}
                      <td className="order_frorm_td order_from_td_pnum order_detail_thead_style">尺码</td>
                  </tr>
                );
            case '2':
                return(
                  <tr className="order_from_tr">
                      <td className="order_frorm_td order_from_td_num "><TotalCheckBox ref={'checkall'} checkall={this.checkall.bind(this)}/></td>
                      {/*<td className="order_frorm_td order_from_td_snum order_detail_thead_style">款号</td>*/}
                      <td className="order_frorm_td order_from_td_cnum order_detail_thead_style">品名</td>
                      <td className="order_frorm_td order_from_td_describe order_detail_thead_style">描述</td>
                      <td className="order_frorm_td order_from_td_size order_detail_thead_style">规格</td>
                      <td className="order_frorm_td order_from_td_monad order_detail_thead_style">单位</td>
                  </tr>
                );

            case '3':
                return(
                  <tr className="order_from_tr">
                      <td className="order_frorm_td order_from_td_num "><TotalCheckBox ref={'checkall'} checkall={this.checkall.bind(this)}/></td>
                      {/*<td className="order_frorm_td order_from_td_snum order_detail_thead_style">款号</td>*/}
                      <td className="order_frorm_td order_from_td_cnum order_detail_thead_style">品名</td>
                      <td className="order_frorm_td order_from_td_size order_from_td_size_big order_detail_thead_style">大货克重要求</td>
                      <td className="order_frorm_td order_from_td_size order_detail_thead_style">幅宽</td>
                      <td className="order_frorm_td order_from_td_size order_detail_thead_style">规格</td>
                        {
                          // <td className="order_frorm_td order_from_td_size order_detail_thead_style">颜色</td>
                        }
                      <td className="order_frorm_td order_from_td_size order_detail_thead_style">纱支</td>
                      <td className="order_frorm_td order_from_td_size order_detail_thead_style">织物组织</td>
                      <td className="order_frorm_td order_from_td_size order_detail_thead_style">密度</td>
                  </tr>
                );
            case '4':
                return(
                  <tr className="order_from_tr">
                      <td className="order_frorm_td order_from_td_num "><TotalCheckBox ref={'checkall'} checkall={this.checkall.bind(this)}/></td>
                      {/*<td className="order_frorm_td order_from_td_snum order_detail_thead_style">款号</td>*/}
                      <td className="order_frorm_td order_from_td_cnum order_detail_thead_style">品名</td>
                      <td className="order_frorm_td order_from_td_describe order_detail_thead_style">描述</td>
                      <td className="order_frorm_td order_from_td_describe order_detail_thead_style">颜色</td>
                      <td className="order_frorm_td order_from_td_img order_detail_thead_style">花型图片</td>
                      <td className="order_frorm_td order_from_td_size order_detail_thead_style">规格</td>
                      <td className="order_frorm_td order_from_td_monad order_detail_thead_style">单位</td>
                      <td className="order_frorm_td order_from_td_ucost order_detail_thead_style">单价</td>
                      <td className="order_frorm_td order_from_td_pnum order_detail_thead_style">数量</td>
                      <td className="order_frorm_td order_from_td_moeny order_detail_thead_style">金额</td>
                      <td className="order_frorm_td order_from_td_size order_detail_thead_style">备注</td>
                  </tr>
                );
            case '5':
                return(
                  <tr className="order_from_tr">
                      <td className="order_frorm_td order_from_td_num "><TotalCheckBox ref={'checkall'} checkall={this.checkall.bind(this)}/></td>
                      {/*<td className="order_frorm_td order_from_td_snum order_detail_thead_style">款号</td>*/}
                      <td className="order_frorm_td order_from_td_cnum order_detail_thead_style">品名</td>
                      <td className="order_frorm_td order_from_td_describe order_detail_thead_style">描述</td>
                      <td className="order_frorm_td order_from_td_size order_detail_thead_style">规格</td>
                      <td className="order_frorm_td order_from_td_size order_detail_thead_style">纱线类型</td>
                      <td className="order_frorm_td order_from_td_size order_detail_thead_style">成纱状态</td>
                      <td className="order_frorm_td order_from_td_size order_detail_thead_style">成分</td>
                      <td className="order_frorm_td order_from_td_size order_detail_thead_style">纱支</td>
                  </tr>
                );
        }
    }


    closemodal(){
        this.setState({
            visible:false,
        });
        this.refs.checkall.setState({
            ischeck: false
        })
    }

    afterClose(){
        this.orders = [];
    }

    addmodelitem(){
        let goodsInfoDescModelList = this.state.goodsInfoDescModelList?this.state.goodsInfoDescModelList:[];
        return goodsInfoDescModelList.map((context,index)=>{
            return(

                <AaaESMItem key={ReactKey.key} context={context} ref={index+'check'} ischeckall={this.ischeckall.bind(this)} ordertype_num = {this.props.ordertype_num}/>
            );
        });
    }

    sure(){
        this.props.addModalfromlist(this.orders);
        this.refs.checkall.setState({
            ischeck: false
        })
    }


    checkall(is){
        this.orders = [];
        this.state.goodsInfoDescModelList.map((context,index)=>{
            let AddItem = this.refs[index+'check'];
            if(is){
                AddItem.chose();
                this.orders.push(context);
            }else{
                AddItem.unchose();
                this.orders= [];
            }

        });
    }

    ischeckall(context){
        let total = 0;
        this.state.goodsInfoDescModelList.map((context,index)=>{
            let AddItem = this.refs[index+'check'];
            total = total+AddItem.int;
        });
        if(total == 0){
            this.refs.checkall.chose();
        }else{
            this.refs.checkall.unchose();
        }
        if(this.orders.indexOf(context)>-1){
            Util.removedarrayitem(this.orders,context)
        }else{
            this.orders.push(context)
        }
    }


    render(){
        return (
            <Modal wrapClassName="addModalfromlist_style" visible={this.state.visible} footer={null} title={'导入商品'} onCancel={this.closemodal.bind(this)} width={1200} style={{left: '5%'}} afterClose={this.afterClose.bind(this)}>
                <table className="order_form_table">
                    <thead>
                      {
                        this.viewHead()
                      }
                    </thead>
                    {
                        this.addmodelitem()
                    }
                </table>

                <div className="order_modal_bottom_div">
                    <span className="order_modal_bottom_span_sure" onClick={this.sure.bind(this)}>确认</span>
                    <span className="order_modal_bottom_span_cancle" onClick={this.closemodal.bind(this)}>取消</span>
                </div>
                <div style={{height:60}}/>


            </Modal>
        )
    }
}



class TotalCheckBox extends React.Component{
    constructor(props){
        super(props);
        this.state={
            ischeck:false
        }
    }

    chose(){
        this.setState({
            ischeck:true
        });
    }

    unchose(){
        this.setState({
            ischeck:false
        });
    }


    checkallchange(e){
        let is = e.target.checked;
        if(is){
            this.setState({
                ischeck:true,
            });
        }else{
            this.setState({
                ischeck:false,
            });
        }

        this.props.checkall(is);

    }

    componentWillMount(){
    }

    render(){
        return(
            <Checkbox onChange={this.checkallchange.bind(this)} checked={this.state.ischeck}/>
        );
    }
}
